package com.atguigu.yygh.order.controller;

import com.atguigu.yygh.common.helper.JwtHelper;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.service.OrderService;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: 订单的相关的接口
 * @author: 化一帆
 * @date: 2022/12/8 18:24
 * @version: v1.0
 */
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {
    @Autowired
    private OrderService orderService;

    /**
     * @description: 生成挂号订单
     * 参数：排班id    就诊人id
     * @author 化一帆
     * @date 2022/12/8 18:36
     */
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public R saveOrders(@PathVariable String scheduleId,
                        @PathVariable Long patientId){

        Long orderId =   orderService.saveOrder(scheduleId,patientId);
        return R.ok().data("orderId",orderId);

    }


    @GetMapping("auth/getOrder/{orderId}")
    public R getOrder(@PathVariable Long orderId) {
        OrderInfo orderInfo = orderService.getOrderInfo(orderId);
        return R.ok().data("orderInfo",orderInfo);
    }


    /**
     * @description:  订单页表条件分页
     * @author 化一帆
     * @date 2022/12/9 19:16
     */

    @GetMapping("auth/{page}/{limit}")
    public R list(@PathVariable Long page, @PathVariable Long limit, OrderQueryVo orderQueryVo, HttpServletRequest request) {

        //设置当前用户id【从token中获取userid】
        Long userId = JwtHelper.getUserId(request.getHeader("token"));
        //AuthContextHolder.getUserId(request) 从用户服务下拷贝工具类到订单服务中
        orderQueryVo.setUserId(userId);

        Page<OrderInfo> pageParam = new Page<>(page,limit);
        IPage<OrderInfo> pageModel = orderService.selectPage(pageParam,orderQueryVo);

        return R.ok().data("pageModel",pageModel);
    }


    /**
     * @description:  订单的状态列表
     * @author 化一帆
     * @date 2022/12/9 19:22
     */
    @GetMapping("auth/getStatusList")
    public R getStatusList() {
        return R.ok().data("statusList", OrderStatusEnum.getStatusList());
    }

    /**
     * @description: 取消预约的接口
     * @author 化一帆
     * @date 2022/12/11 19:00
     */

    //点击取消按钮
    @GetMapping("auth/cancelOrder/{orderId}")
    public R cancelOrder(@PathVariable("orderId") Long orderId) {
        Boolean flag = orderService.cancelOrder(orderId);
        return R.ok().data("flag",flag);
    }
}
